package com.milotnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milotnt.constants.TableConstant;
import com.milotnt.mapper.ClassTableMapper;
import com.milotnt.pojo.*;
import com.milotnt.service.ClassOrderService;
import com.milotnt.service.ClassTableService;
import com.milotnt.service.CoachService;
import com.milotnt.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ZhangMing [1157038410@qq.com]
 * @date 2021/8/11
 */

@Service
@Slf4j
public class ClassTableServiceImpl extends ServiceImpl<ClassTableMapper, ClassTable> implements ClassTableService {

    @Autowired
    private ClassOrderService orderService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private CoachService coachService;

    @Override
    public List<ClassTable> findAll() {
        // 未结课的排在前面
        LambdaQueryWrapper<ClassTable> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(ClassTable::getStatus);
        return this.list(lqw);
    }

    @Override
    public List<ClassTable> findNotStart() {
        LambdaQueryWrapper<ClassTable> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassTable::getStatus, TableConstant.NO_START);
        return this.list(lqw);
    }

    @Override
    public void deleteClassByClassId(Integer classId) {
        LambdaQueryWrapper<ClassOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassOrder::getClassId, classId);
        orderService.remove(lqw);
        this.removeById(classId);
        taskService.cancelTask(classId);
    }

    @Override
    public Boolean insertClass(ClassTable classTable) {
        classTable.setClassBegin(classTable.getClassBegin().replace("T", " "));
        boolean b = this.save(classTable);
        // 添加任务
        taskService.addTask(classTable);
        return b;
    }

    @Override
    public ClassTable selectByClassId(Integer classId) {
        return this.getById(classId);
    }

    // 消费任务，修改当前状态
    @Scheduled(fixedRate = 1000)  // 每秒执行一次
    @Override
    public void updateStatus() {
        log.info("修改状态");
        Task task = taskService.pullTask();
        if(task != null){
            ClassTable table = this.getById(task.getParameters());
            table.setStatus(TableConstant.START);
            this.updateById(table);
        }
    }

    @Override
    public List<ClassTable> recentClass(Integer coachId) {
        LambdaQueryWrapper<ClassTable> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassTable::getCoachId, coachId);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String s = format.format(new Date());
        lqw.gt(ClassTable::getClassBegin, s);
        lqw.orderByAsc(ClassTable::getClassBegin);
        lqw.last("limit 5");
        return this.list(lqw);
    }

    @Override
    public List<ClassTable> findAllByCoachId(Integer coachId) {
        LambdaQueryWrapper<ClassTable> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassTable::getCoachId, coachId);
        lqw.orderByAsc(ClassTable::getStatus);
        return this.list(lqw);
    }
}
